package demoMod.gdxform.helpers;

import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.GlyphLayout;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;

import java.util.HashMap;
import java.util.Map;

public class FontHelper {
    public static GlyphLayout layout = new GlyphLayout();
    private static final Map<String, BitmapFont> fontMap = new HashMap<>();

    public static void registerFont(String fontKey, BitmapFont font) {
        fontMap.put(fontKey, font);
    }

    public static BitmapFont getFont(String fontKey) {
        return fontMap.get(fontKey);
    }

    public static void renderSmartText(SpriteBatch sb, BitmapFont font, String msg, float x, float y, Color baseColor) {
        renderSmartText(sb, font, msg, x, y, Float.MAX_VALUE, font.getLineHeight(), baseColor);
    }

    public static void renderFontCentered(SpriteBatch sb, BitmapFont font, String msg, float x, float y, Color c) {
        layout.setText(font, msg);
        renderFont(sb, font, msg, x - layout.width / 2.0F, y + layout.height / 2.0F, c);
    }

    public static void renderFont(SpriteBatch sb, BitmapFont font, String msg, float x, float y, Color c) {
        font.setColor(c);
        font.draw(sb, msg, x, y);
    }

    public static void renderSmartText(SpriteBatch sb, BitmapFont font, String msg, float x, float y, float lineWidth, float lineSpacing, Color baseColor) {
        float curWidth = 0.0F;
        float curHeight = 0.0F;
        layout.setText(font, " ");
        float spaceWidth = layout.width;
        curHeight = -(lineSpacing - layout.height) / 2.0F;
        String[] var8 = msg.split(" ");
        for (String s : var8) {
            String word = s;
            if (word.equals("NL")) {
                curWidth = 0.0F;
                curHeight -= lineSpacing;
            } else if (word.equals("TAB")) {
                curWidth += spaceWidth * 5.0F;
            } else {
                Color color = identifyColor(word).cpy();
                if (!color.equals(Color.WHITE)) {
                    word = word.substring(2);
                    color.a = baseColor.a;
                    font.setColor(color);
                } else {
                    font.setColor(baseColor);
                }

                layout.setText(font, word);
                if (curWidth + layout.width > lineWidth) {
                    curHeight -= lineSpacing;
                    font.draw(sb, word, x, y + curHeight);
                    curWidth = layout.width + spaceWidth;
                } else {
                    font.draw(sb, word, x + curWidth, y + curHeight);
                    curWidth += layout.width + spaceWidth;
                }

            }
        }

        layout.setText(font, msg);
    }

    private static Color identifyColor(String word) {
        if (word.length() > 0 && word.charAt(0) == '#') {
            switch (word.charAt(1)) {
                case 'b':
                    return new Color(-2016482305);
                case 'g':
                    return new Color(2147418367);
                case 'p':
                    return new Color(-293409025);
                case 'r':
                    return new Color(-10132481);
                case 'y':
                    return new Color(-272084481);
                default:
                    return Color.WHITE;
            }
        } else {
            return Color.WHITE;
        }
    }
}
